# 输出欢迎信息到控制台
# 运行方式：在VSCode中打开该文件，按F5运行
print("类型转换测试")
a = 10
b = 20.0
c = a + b
d = "Hello Python"

print(type(a))
print(type(b))
print(type(c))

print(d)
d = 30
print(d)
print(type(d))

print("字符串测试")
# 输入变量
name = input("请输入您的姓名：")
print("你好，" + name + "！")

print("条件语句测试")
if name == "小明":
    print("欢迎小明！")
elif name == "小红":
    print("欢迎小红！")
else:
    print("欢迎，" + name + "！")

print("循环语句测试")
for i in range(1, 10):
    print(i)
    if i == 5:
        break
    print("循环中...")
print("循环结束！")

print("函数测试")
import keyword

def is_valid_identifier(identifier):
    # 检查标识符是否为Python的保留字
    if keyword.iskeyword(identifier):
        return False
    
    # 检查标识符的第一个字符是否为字母或下划线
    if not identifier[0].isalpha() and identifier[0] != '_':
        return False
    
    # 检查标识符的剩余字符是否都是字母、数字或下划线
    for char in identifier[1:]:
        if not char.isalnum() and char != '_':
            return False
    
    # 如果通过所有检查，返回True
    return True

# 测试示例
print(is_valid_identifier('my_variable'))  # True
print(is_valid_identifier('2nd_variable'))  # False
print(is_valid_identifier('for'))  # False
print(is_valid_identifier('_validIdentifier123'))  # True

print("字符串操作测试")
str1='123456789'



print("字典操作测试")
# 字典
person = {
    'name': '小明',
    'age': 20,
    'gender': '男'
}

print(person['name'])
print(person['age'])
print(person.get('gender', '未知'))
print(person['gender'])

person['age'] = 21
print(person['age'])
print(person['age'])

# 列表
fruits = ['apple', 'banana', 'orange']
print(fruits)

fruits.append('grape')
print(fruits)

fruits.insert(1, 'peach')
print(fruits)

fruits.remove('banana')
print(fruits)

fruits.pop(1)
print(fruits)


print("元组测试")
# 元组
coordinates = (3, 4)
print(coordinates[0])
print(coordinates[1])

# 集合
print("集合测试")
numbers = {1, 2, 3, 4, 5}
print(numbers)

numbers.add(6)
print(numbers)

# 异常处理
print("异常处理测试")
try:
    age = int(input("请输入您的年龄："))
    if age < 0:
        raise ValueError("年龄不能为负数！")
    print("你已满" + str(age) + "周岁！")
except ValueError as e:
    print(e)